/**
 * 资料&说明页面（管理员）JS
 *
 * Created by Zmt on 2015/11/04.
 */
var storeUrl;

var isUser = null;

$().ready(function () {
    isUser = (3 == $("#u_type").val());
    if (isUser)
        $("#toolbar").hide();
});

/**
 * 搜索
 */
function search()
{
    $("#dg").datagrid("load", {
        name: $("#s_name").val(),
        type: $("#s_type").combobox("getValue"),
        vCode: $("#s_code").val()
    });
}

function isOperate(value, row, index)
{
    var button = "<button href=\"javascript:void(0)\" class=\"easyui-linkbutton\" iconCls=\"icon-ok\" plain=\"true\"  onclick=\"download('"+index+"')\">下载</button>&nbsp;&nbsp;";
    if (null != isUser && !isUser)
    {
        var bt_modify = "<button href=\"javascript:void(0)\" class=\"easyui-linkbutton\" iconCls=\"icon-ok\" plain=\"true\"  onclick=\"modify('"+index+"')\">编辑 </button>&nbsp;&nbsp;";
        button = bt_modify + button;
    }
    return button;
}

function create()
{
    $('#dlg_upload').dialog('open').dialog('setTitle', '添加文件');
    $('#fm').form('clear');
    $("#fm_file").form('clear');
    $('input').removeAttr("disabled");
    $('textarea').removeAttr("disabled");
    $('#store').show();
    storeUrl = '/data/create.do';
}

function modify(index)
{
    $("#fm_file").form("clear");
    //$("#fileToUpload").val(null);
    $("#dg").datagrid("selectRow", index);
    var row = $("#dg").datagrid("getSelected");
    var id = row.id;
    if (row)
    {
        var pvCode = row.pvCode;
        $('#dlg_upload').dialog('open').dialog('setTitle', '修改文件');//弹框的标题
        $('#fm').form('clear');//表单清空内容
        $('#fm').form('load', row);//向表单里面填充数据
        $('input').removeAttr("disabled");//去除input元素的disabled属性
        $('textarea').removeAttr("disabled");//去除textarea的不可编辑属性
        if (0 == pvCode || null == pvCode)
            $("#pvCode").val("");
        $('#modify').show();
        storeUrl = '/data/modify.do?id=' + id;
    }
}

function detail()
{
    var row = $("#dg").datagrid("'getSelected'");
    if (row)
    {
        $("#dlg_upload").dialog("open").dialog("setTitle", "文件详情");
        $("#fm").form("load", row);
        $("input").attr("disabled", "disabled");
        $("textarea").attr("disabled", "disabled");
        $("#store").hide();
    }
}

function store()
{
    $('#fm').form('submit', {
        url: storeUrl,
        onSubmit: function () {
            return $(this).form('validate');
        },
        success: function (result) {
            var result = $.parseJSON(result);
            if (result.ok)
            {
                $.messager.show({
                    title: '成功',
                    msg: "保存成功"
                });
                $('#dlg_upload').dialog('close');
                $('#dg').datagrid('reload');
            }
            else
            {
                $.messager.alert("提示信息", result.msg);
            }
        }
    });
}

function cancel()
{
    $("input").removeAttr("disabled");
    $("#dlg_upload").dialog("close");

}

function deleteData()
{
    var checkedItems = $("#dg").datagrid("getChecked");
    if ("" != checkedItems)
    {
        var ids = [];
        $.each(checkedItems, function (index, item) {
            if ("" != item.id)
                ids.push(item.id);
        });
        if (ids.length != 0)
        {
            $.messager.confirm("删除确认", "删除后将无法恢复，您确定要删除吗？", function (r) {
                if (r)
                {
                    $.post("/data/delete.do?ids=" + ids, function (result) {
                        if (result.ok)
                        {
                            $.messager.show({title: "提示信息", msg: "删除成功!"});
                            $("#dg").datagrid("reload");
                        }
                        else
                            $.messager.alert("提示信息", result.msg);
                    }, "json");
                }
            });
        }
    }
    else
        $.messager.alert("提示信息", "未选中任何文件！");
}

//检查文件后缀 上传文件
function uploadFile()
{
    var file = document.getElementById('fileToUpload').files[0];
    if (file == null)
        return;
    var fileName = file.name;
    var suffix = fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length);
    $("#fm_file").form("submit", {
        url: "/data/uploadFile.do",
        onSubmit: function ()
        {
            return $(this).form("validate");
        },
        success: function (result) {
            var result = $.parseJSON(result);
                if (result.ok)
                {
                    var filePath = result.data;
                    $("#path").val(filePath);
                    $("#suffix").val(suffix);
                }
                else
                    $.messager.alert("提示信息", result.msg);
            }
        });
}

/**
 * 下载
 *
 * @param index
 */
function download(index)
{
    $("#dg").datagrid("selectRow", index);
    var row = $("#dg").datagrid("getSelected");
    var id = row.id;
    if (row)
        window.location.href = "/data/download.do?id=" + id;
}
